@isTest
private class OpportunityHandlerTest {
    static testMethod void testMethod1() 
    {

        Country__c country = TestUtils.createCountry();
        country.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('OldCountryArchive','Country__c');
        insert country;

        // CharacteristicMedicalMarket__c  cmm = TestUtils.createCharacteristicMedicalMarket();
        // cmm.Country__c = country.Id;
        // insert cmm;
         Product__c p = TestUtils.createProduct();
         p.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        insert p ;

        MedicalGrade__c medicalgrade = TestUtils.createMedicalGrade();
        medicalgrade.Country__c = country.Id;
        insert medicalgrade;

        IVDWholeMarket__c ivdwm = TestUtils.createIVDWholeMarket();
        ivdwm.Country__c = country.Id; 

        IVDProductLineMarket__c ivdplm = TestUtils.createIVDProductLineMarket();
        ivdplm.Country__c = country.Id;
        insert ivdplm;


        Province__c province = TestUtils.createProvince();
        insert province;

        City__c city = TestUtils.createCity();
        city.Province__c = province.Id;
        insert city;

        District__c district = TestUtils.createDistrict();
        district.City__c = city.Id;
        insert district;

        Account acc = TestUtils.createAccount();
        acc.Name = '客户A';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('DealerAbroad','Account');
        //acc.Country__c = country.Id;
        acc.District__c = district.Id;
        acc.Province__c = province.Id;
        acc.City__c = city.Id;
        acc.SpecialDepart__c = '内科_呼吸内科; 内科_神经内科';
        acc.CustomerAttribute__c = '公立医疗机构';
        acc.Level__c = '一级医院';
        insert acc;

        /*Department__c dt1=[select id,hospitalDepartments__c,TerminalCustomer__c from Department__c where 
        hospitalDepartments__c='检验科' and TerminalCustomer__c=:acc.Id limit 1];
        dt1.LastYearRevenue__c=123;
        update dt1;
        */

        //Opportunity opp = TestUtils.createOpportunity();
        //opp.Name = '业务机会A';
        //opp.AccountId = acc.Id;
        //opp.StageName = '意向';
        //opp.Department__c = '检验科';
        //opp.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('CustomerOpportunity','Opportunity');
        //insert opp;

        Test.startTest();

        Opportunity opp = TestUtils.createOpportunity();
        
        opp.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('OverseaDearlerOpportunity','Opportunity');

        opp.AccountId = acc.Id;
        opp.StageCanChange__c = '意向';
        opp.StageName = '意向';
        insert opp;

        //填写完整客户信息
        // acc.CustomerAttribute__c = '公立医疗机构';
        // acc.Level__c = '一级甲等';
        // acc.SpecialDepart__c = '内科_呼吸内科; 内科_神经内科';
        // update acc;
        
        //增加意向产品
        IntentProduct__c ip = TestUtils.createIntentProduct();
        ip.Opportunity__c = opp.Id;
        ip.Product__c  = p.Id;
        insert ip;

       /* Department__c dep = [select Id, Name, hospitalDepartments__c, LastYearRevenue__c from Department__c where TerminalCustomer__c =: acc.Id];
        System.debug('OHTest: '+dep);

        dep.Name='测试科室';
        dep.hospitalDepartments__c = '检验科';
        dep.LastYearRevenue__c = 20000;
        update dep;*/

        //联系人补充
        /*Contact con1 = TestUtils.createContact();
        con1.LastName = '测试采购科';
        con1.AccountId = acc.Id;
        con1.Post__c = '科长/主任';
        System.debug('OpportunityHandler==>'+con1);
        insert con1;

        Contact con2 = TestUtils.createContact();
        con2.LastName = '测试分管院长';
        con2.Post__c = '分管院长(设备)';
        con2.Departments__c = '医院办公室';
        con2.AccountId = acc.Id;
        insert con2;

        Contact con3 = TestUtils.createContact();
        con3.LastName = '测试财务';
        con3.Post__c = '科长/主任';
        con3.Departments__c = '财务科/计财处';
        con3.AccountId = acc.Id;
        insert con3;
    */
        opp.StageName = '立项';
        try{
           update opp;
        }catch(Exception e){
            System.debug('OHTest: '+e.getMessage());
        }
        System.assertEquals('立项',opp.StageName);

         //增加意向产品
        IntentProduct__c ip1 = TestUtils.createIntentProduct();
        ip1.Opportunity__c = opp.Id;
        ip1.Product__c  = p.Id;

        // ip1.Category1__c = '免疫';
        insert ip1;

       // System.assertEquals('多项目' , opp.ProjectType__c);

        IntentProduct__c ip2 = TestUtils.createIntentProduct();
        ip2.Opportunity__c = opp.Id;
        ip2.Product__c  = p.Id;
        // ip2.Category1__c = '生化';
        insert ip2;

        IntentProduct__c ip3 = TestUtils.createIntentProduct();
        ip3.Opportunity__c = opp.Id;
        ip3.Product__c  = p.Id;

        // ip3.Category1__c = '微生物';
        insert ip3;

        IntentProduct__c ip4 = TestUtils.createIntentProduct();
        ip4.Opportunity__c = opp.Id;
        ip4.Product__c  = p.Id;

        // ip4.Category1__c = '分子';
        insert ip4;

        List<IntentProduct__c> ipList = [select Id from IntentProduct__c where Opportunity__c =: opp.Id];
        System.debug('OHT：'+ipList);

        opp = [select Id, Name, ProjectType__c, RecordTypeId, StageName  from Opportunity where Id =: opp.Id];
        System.debug('OHT：'+opp);

       //  System.assertEquals('整体打包' , opp.ProjectType__c);

        delete ip4;
        Country__c newCountry = TestUtils.createCountry();
        newCountry.Country__c= '美国';
        newCountry.Population__c= '4亿';
        newCountry.RecordTypeId=Utils.getRecordTypeIdByDevNameAndObjName('OldCountryArchive', 'Country__c');
        insert newCountry;

        opp.Country__c=newCountry.Id;
        update opp;

        Test.stopTest();

    }    
}